package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.enums.OaIntegralRuleEnum;
import com.cheer.service.*;
import com.cheer.vo.MallCategoryVO;
import com.cheer.vo.MallHomeVO;
import com.cheer.vo.MallImageLinkVO;
import com.cheer.vo.SimpleObjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@Api(tags = "商城首页")
@RequestMapping("/home")
public class MallHomeController {

    @Autowired
    private MallImageService mallImageService;

    @Autowired
    private MallCategoryService mallCategoryService;

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private OaIntegralService oaIntegralService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private AuthService authService;

    @Autowired
    private TaskService taskService;

    @GetMapping
    @ApiOperation("商城首页 小红花 TODO")
    public Result<MallHomeVO> home(HttpSession session) {
        MallHomeVO home = new MallHomeVO()
                .setTopImageList(mallImageService.listTopImages())
                .setWatermarkImage(mallImageService.getWatermarkImage());

        // 积分兑换任务
        String openid = authService.getOpenid(session);
        Integer userId = authService.getUserId(session);
        taskService.execTask(userId, openid, OaIntegralRuleEnum.EXCHANGE);
        // 查询分类
        List<MallCategoryVO> categoryList = mallCategoryService.listAllShow();
        home.setCategoryList(categoryList);

        // 查询第一个分类的商品
        if (CollectionUtils.isNotEmpty(categoryList)) {
            home.setProductList(mallProductService.listByCategoryId(1, 10, categoryList.get(0).getId()));
        }

        return Result.success(home);
    }
}
